/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseManeger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import me.aviator.entity.HibernateUtil;
import me.aviator.entity.Ribbon;
import me.aviator.entity.myColor;
import me.aviator.service.impl.RibbonService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class RibbonFormController implements Initializable {
    @FXML
    private AnchorPane RootPane;
    @FXML
    private TextField RibbonPath;
    @FXML
    private Button BtnOpen;
    @FXML
    private Button BtnClose;
    @FXML
    private Button BtnAdd;
    
    private String RibbonPathString=null;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void HandleBtnOpen(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("打开色带文件");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); 
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("色带文件", "*.txt")
        );
        Stage s = new Stage(); 
        File file = fileChooser.showOpenDialog(s);
        if (file != null) {
            String SVGPathString=file.getPath();
            RibbonPath.setText(SVGPathString);
        }
    }

    @FXML
    private void HandleBtnClose(ActionEvent event) {
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnAdd(ActionEvent event) throws FileNotFoundException, IOException {
        SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        Session sess=sessionFactory.openSession();
        Transaction tx=sess.beginTransaction();
        
        RibbonService ribbonService=new RibbonService(sess);
        
        RibbonPathString=RibbonPath.getText();
        File file=new File(RibbonPathString);
        BufferedReader reader=new BufferedReader(new FileReader(file));
        String line=reader.readLine();
        int num=Integer.parseInt(line.substring(12, line.length()-1));
        int count=0;
        while((line=reader.readLine())!=null&&!line.equals("end")){
            count++;
            String catalog="";
            String name="";
            String tone="";
            List<myColor> colorlist=new ArrayList<>();
            line=line.substring(4,line.length()-1);
            String s=line.substring(0, 1);
            if("0".equals(s)){
                catalog="质别";
            }
            else{
                catalog="级别";
            }
             s=line.substring(1, 3);
            tone=s;
            for(int i=0;i<num;i++)
            {
                myColor color=new myColor();
                line=reader.readLine();
                String[] lines=line.split(",");
                color.setRed(Integer.parseInt(lines[0]));
                color.setGreen(Integer.parseInt(lines[1]));
                color.setBlue(Integer.parseInt(lines[2]));
                colorlist.add(color);
            }
            ribbonService.uploadRibbon(name, tone, catalog, num, colorlist);
            sess.flush();
            sess.clear();
            line=reader.readLine(); 
        }
        tx.commit();
        sess.close();
        sessionFactory.close();
        reader.close();
        RootPane.getScene().getWindow().hide();
    }
    
    
}
